package com.stlm2.dems.service;

import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.Stages;

import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.GoodSpecification;
import com.stlm2.dems.dao.GoodSpecificationDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("goodSpecificationService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class GoodSpecificationService extends BaseServiceImpl<GoodSpecification> {

	@Resource
	private GoodSpecificationDao goodSpecificationDao;
	@Resource
	private DdmGoodsService ddmGoodsService;
	@Resource
	private UserService userService;
	@Resource
	private StagesService stagesService;

	@Override
	protected BaseDao <GoodSpecification> getDao() {
		return goodSpecificationDao;
	}

    public GoodSpecification getGoodSpecification(Map<String, Object> map) {

		GoodSpecification goodSpecification = new GoodSpecification();
		String propertiesId = (String) map.get("propertiesid");
		String args[] = propertiesId.split(",");
        if(args.length==1){
				return goodSpecificationDao.getGoodSpecification(map);

		}
		else if(args.length==2){

			goodSpecification = goodSpecificationDao.getGoodSpecification(map);


			if(goodSpecification==null){
				Map<String,Object> paramMap = new HashMap<String, Object>();
				paramMap.put("propertiesid",args[1]+","+args[0]);
				paramMap.put("goodid",(Integer)map.get("goodid"));
				goodSpecification = goodSpecificationDao.getGoodSpecification(paramMap);
              return  goodSpecification;
			}else{
				return  goodSpecification;

			}

		}else if(args.length==3){
			goodSpecification = goodSpecificationDao.getGoodSpecification(map);// 123
                if(goodSpecification!=null){
                    return  goodSpecification;
				}else{
					String args3[] = propertiesId.split(",");
				    Map<String,Object> map132 = new HashMap<String, Object>();
					map132.put("propertiesid",args[0]+","+args[2]+","+args[1]);
					map132.put("goodid",(Integer)map.get("goodid"));
					goodSpecification = goodSpecificationDao.getGoodSpecification(map132);//132
					if(goodSpecification!=null){
						 return goodSpecification;

					}else {
						Map<String,Object> map213 = new HashMap<String, Object>();
						 map213.put("propertiesid",args3[1]+","+args3[0]+","+args3[2]);
						 map213.put("goodid",(Integer)map.get("goodid"));
                        goodSpecification = goodSpecificationDao.getGoodSpecification(map213);//213
						if (goodSpecification!=null) {
                             return  goodSpecification;
 						}else {
							Map<String,Object> map231 = new HashMap<String, Object>();
							map231.put("propertiesid",args3[1]+","+args3[2]+","+args3[0]);
							map231.put("goodid",(Integer)map.get("goodid"));
							goodSpecification = goodSpecificationDao.getGoodSpecification(map231);//231
							if (goodSpecification!=null) {
								 return goodSpecification;
							}else {
								Map<String,Object> map312 = new HashMap<String, Object>();
								map312.put("propertiesid",args3[2]+","+args3[0]+","+args3[1]);
								map312.put("goodid",(Integer)map.get("goodid"));
								goodSpecification = goodSpecificationDao.getGoodSpecification(map312);//312
								if (goodSpecification!=null) {
									 return goodSpecification;
								}else{

									Map<String,Object> map321 = new HashMap<String, Object>();
									map321.put("propertiesid",args3[2]+","+args3[1]+","+args3[0]);
									map321.put("goodid",(Integer)map.get("goodid"));
									goodSpecification = goodSpecificationDao.getGoodSpecification(map321);//321
									if (goodSpecification!=null) {
										return goodSpecification;
									}else {
										return null;

									}
								}
							}
						}
					}
				}
		}
		return null;
    }

    public List<GoodSpecification> getByGoodId(Integer goodid) {
		return goodSpecificationDao.getGoodSpecificationList(goodid);
    }

	public List<ModelMap> conventAll2(List<GoodSpecification> goodSpecifications) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(GoodSpecification gs:goodSpecifications){
			list.add(builderModelAll2(gs));
		}
		return list;
	}

	public ModelMap builderModelAll2(GoodSpecification gs) {
		ModelMap map=new ModelMap();
		map.put("id",gs.getId());
		map.put("name",gs.getPropertiesName());
		return map;
	}

	public List<ModelMap> conventAll(List<GoodSpecification> goodSpecifications) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(GoodSpecification gs:goodSpecifications){
			list.add(builderModelAll(gs));
		}
		return list;
	}

	public ModelMap builderModelAll(GoodSpecification gs) {
		ModelMap map=new ModelMap();
		DdmGoods goods = ddmGoodsService.findEntityById(gs.getGoodId());
		Stages stages=new Stages();
		stages.setSpecificationId(gs.getId());
		List<Stages> stagesList=stagesService.getList(-1,-1,stages);
		User user=userService.getById(gs.getCreateUserId());
		map.put("id",gs.getId());
		map.put("propertiesId",gs.getPropertiesId());
		map.put("propertiesName",gs.getPropertiesName());
		map.put("image",gs.getImage());
		map.put("memberPrice",gs.getMemberPrice());
		map.put("ddPrice",gs.getDdPrice());
		map.put("goodState",gs.getGoodsState().toString());
		map.put("goodName",goods.getName());
		map.put("createTime", Convert.toFullYMDHMS(gs.getCreateTime()));
		map.put("createUserName",user.getRealName()!=null?user.getRealName():user.getName());
		map.put("stages",stagesList);
		map.put("parameter",gs.getParameter()!=null?gs.getParameter():"");
		return map;
	}
	public ModelMap builderModel(GoodSpecification gs) {
		ModelMap map=new ModelMap();
		map.put("id",gs.getId());
		map.put("image",gs.getImage());
		map.put("ddPrice",gs.getDdPrice());
		map.put("costPrice",gs.getCostPrice());
		map.put("purchasePrice",gs.getPurchasePrice());
		map.put("memberPrice",gs.getMemberPrice());
		map.put("parameter",gs.getParameter()!=null?gs.getParameter():"");
		return map;
	}

    public void updatePropertiesName(Map<String, String> map) {
		goodSpecificationDao.updatePropertiesName(map);
    }
}